package com.atguigu.KMP;

/**
 * @author tbwtbw
 * @create 2021-11-25 14:38
 */
public class ViolateMatch {
    public static void main(String[] args) {

        String str1 = "北京上海天津广州";
        String str2 = "海天";
        int i = violateMatch(str1, str2);
        System.out.println(i);
    }

    public static int violateMatch(String str1,String str2){

        int i = 0;//指向str1
        int j = 0;

        while (i < str1.length() && j < str2.length()){
            if (str1.charAt(i) == str2.charAt(j)){
                i++;
                j++;
            }else {
                i = i - j + 1;
                j = 0;
            }
        }

        if (j == str2.length())
            return i - j;
        return  -1;
    }
}
